using System;
using System.Data;
using System.Data.SqlClient;

namespace SLIITPro.Core
{
    public class Datamanager
    {
        private SqlConnections _conn;
        public DataTable ExecuteQuery(string query, string tableName, SqlParameter[] parameters)
        {
            _conn = new SqlConnections();
            try
            {
                var com = new SqlCommand(query, _conn.CreateConn());
                if (parameters != null) com.Parameters.AddRange(parameters);
                var myAdapter = new SqlDataAdapter(com);
                var ds = new DataSet();
                myAdapter.Fill(ds, tableName);
                ds.Tables[0].TableName = tableName;
                return ds.Tables[0];
            }
            catch (Exception ex) { throw ex; }
            finally { _conn.CloseConn(); }
        }

        public void ExecuteNonQuery(string query, SqlParameter[] parameters)
        {
            _conn = new SqlConnections();
            try
            {
                var myCommand = new SqlCommand(query, _conn.CreateConn());
                if (parameters != null) myCommand.Parameters.AddRange(parameters);
                myCommand.ExecuteNonQuery();
            }
            catch (Exception ex) { throw ex; }
            finally { _conn.CloseConn(); }
        }

        public SqlDataReader ExecuteReader(string query, SqlParameter[] parameters)
        {
            _conn = new SqlConnections();
            try
            {
                var myCommand = new SqlCommand(query, _conn.CreateConn());
                if (parameters != null) myCommand.Parameters.AddRange(parameters);
                var reader = myCommand.ExecuteReader();
                return reader;
            }
            catch (Exception ex) { throw ex; }
            finally { _conn.CloseConn(); }
        }
    }
}